package com.cloudOffice.controller;

import com.cloudOffice.model.system.SysMenu;
import com.cloudOffice.result.Result;
import com.cloudOffice.service.SysMenuService;
import com.cloudOffice.vo.system.AssignMenuVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @program: cloud-office
 * @className: SysMenuController
 * @description: 菜单管理
 * @data: 2024/2/8 15:14
 * @author: ihu
 * @version: 1.0
 **/

@Slf4j
@Tag(name = "菜单管理")
@RestController("SysMenuController")
@RequestMapping("/admin/system/sysMenu")
public class SysMenuController {
	
	
	@Resource
	private SysMenuService sysMenuService;
	
	/**
	 * 获取菜单
	 */
	@GetMapping("findNodes")
	@Operation(summary = "获取菜单")
	public Result<List<SysMenu>> findNodes() {
		log.info("获取菜单");
		return Result.success(sysMenuService.findNodes());
	}
	
	/**
	 * 新增菜单
	 */
	@PostMapping("save")
	@Operation(summary = "新增菜单")
	public Result<?> save(@RequestBody SysMenu permission) {
		log.info("新增菜单：permission: {}", permission);
		sysMenuService.save(permission);
		return Result.success();
	}
	
	/**
	 * 修改菜单
	 */
	@PutMapping("update")
	@Operation(summary = "修改菜单")
	public Result<?> updateById(@RequestBody SysMenu permission) {
		log.info("修改菜单：permission: {}", permission);
		sysMenuService.updateById(permission);
		return Result.success();
	}
	
	/**
	 * 删除菜单
	 */
	@Operation(summary = "删除菜单")
	@DeleteMapping("remove/{id}")
	public Result<?> deleteId(@PathVariable Long id) {
		log.info("删除菜单：id: {}", id);
		sysMenuService.deleteId(id);
		return Result.success();
	}
	
	/**
	 * 根据角色获取菜单权限数据
	 */
	@GetMapping("toAssign/{roleId}")
	@Operation(summary = "根据角色获取菜单权限数据")
	public Result<List<SysMenu>> toAssign(@PathVariable Long roleId) {
		log.info("根据角色获取菜单权限数据：roleId: {}", roleId);
		return Result.success(sysMenuService.findSysMenuByRoleId(roleId));
	}
	
	/**
	 * 给角色分配权限
	 */
	@PostMapping("/doAssign")
	@Operation(summary = "给角色分配权限")
	public Result<?> doAssign(@RequestBody AssignMenuVo assignMenuVo) {
		log.info("给角色分配权限：assignMenuVo: {}", assignMenuVo);
		sysMenuService.doAssign(assignMenuVo);
		return Result.success();
	}
}
